"""
免费体检项目
步骤：
首先判断用户和项目是否存在
y:继续判断
    判断用户是否符合预约该项目的资格
    Y：继续判断
        判断用户是否预约过（根据用户id查询）
        Y：继续判断
            免费预约次数
                Y：继续判断
                    预约时间是否在符合范围内
                    Y：继续判断
                        该时间段是否约满
                            Y：响应已经约满了
                            N：插入数据 响应成功
                    N：响应不在时间段内 重新选择时间
                N：响应超过免费预约次数
        N：继续判断
            预约时间是否在符合范围内
            Y:继续判断
                该时间段是否约满
                    Y：响应已经约满了
                    N：插入数据 响应成功
            N：响应不在时间段内 重新选择时间
    N：响应用户没有改项目资格
N: 响应不存在时结果
class BookingRegisterView(APIView):
    def post(self, request, *args, **kwargs):
        ID_card = request.POST['ID_card']  # 预约人身份证号
        project_id = request.POST['project_id']  # 预约项目id
        time = request.POST['time']  # 预约时间

        # 查询是否可以预约
        a = ReservationOrderModel.objects.filter(project_id=project_id)
        a_json = json.loads(serializers.serialize('json', a))
        number = a_json[0]['fields']['reservation_number']
        r_time = a_json[0]['fields']['r_time']
        is_user = UserModel.objects.filter(u_IDcard=ID_card)
        if is_user.count() == 0:
            data = {
                'msg': "请检查用户您的输入信息",
                'code': 4000,
            }
            return Response(data)
        else:
            if a.count() == 0:
                project = ProjectModel.objects.filter(p_id=project_id)
                project_json = json.loads(serializers.serialize('json', project))
                # print(project_json)
                start_time = project_json[0]['fields']['start_age']
                end_time = project_json[0]['fields']['end_age']
                p_sex = project_json[0]['fields']['c_sex']
                # 免费次数
                free_number = project_json[0]['fields']['free_number']
                # 预交费用
                # p_prepay = project_json[0]['fields']['p_prepay']
                # 提前预约天数
                p_day = project_json[0]['fields']['p_day']

                sex = GetInformation(ID_card).get_sex()
                age = GetInformation(ID_card).get_age()

                if is_check(start_time, end_time, age) and is_sex(p_sex, sex):
                    if r_time == time:
                        if int(number) == 0:
                            order_number = getrand()
                            ReservationOrderModel.objects.create(r_order_number=order_number, reservation_number=1,
                                                                 r_IDcard=ID_card, project_id=project_id, r_time=time)
                            data = {
                                'msg': "预约成功",
                                'code': 2000,
                            }
                            return Response(data)
                        elif int(number) < 5:
                            order_number = getrand()
                            ReservationOrderModel.objects.create(r_order_number=order_number,
                                                                 reservation_number=(int(number) + 1),
                                                                 r_IDcard=ID_card, project_id=project_id, r_time=time)
                            data = {
                                'msg': "预约成功",
                                'code': 2000,
                            }
                            return Response(data)
                        else:
                            data = {
                                'msg': "预约满了",
                                'code': 4000,
                            }
                            return Response(data)
                    else:
                        # 判断是否在预约天数之内
                        data = {
                            'msg': "预约成功",
                            'code': 2001,
                        }
                        return Response(data)


                else:
                    data = {
                        'msg': "请查阅该项目使用人群",
                        'code': 4001,
                    }
                    return Response(data)
            else:
                is_t = ReservationOrderModel.objects.filter(project_id=project_id, r_time=time)
                if len(is_t) == 0:
                    pass
                else:
                    if int(number) == 0:
                        order_number = getrand()
                        ReservationOrderModel.objects.create(r_order_number=order_number, reservation_number=1,
                                                             r_IDcard=ID_card, project_id=project_id, r_time=time)
                        data = {
                            'msg': "预约成功",
                            'code': 2000,
                        }
                        return Response(data)
                    elif int(number) < 5:
                        order_number = getrand()
                        ReservationOrderModel.objects.create(r_order_number=order_number,
                                                             reservation_number=(int(number) + 1),
                                                             r_IDcard=ID_card, project_id=project_id, r_time=time)
                        data = {
                            'msg': "预约成功",
                            'code': 2000,
                        }
                        return Response(data)
                    else:
                        data = {
                            'msg': "预约满了",
                            'code': 4000,
                        }
                        return Response(data)

"""